package top.psjj.news.web;

import top.psjj.news.po.TType;
import top.psjj.news.service.TypeService;
import top.psjj.news.service.impl.TypeServiceImpl;
import top.psjj.news.utils.ServiceProxyFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther:小陈的Java
 */
@WebServlet("/type")
public class TypeServlet extends BasicServlet{
    private TypeService typeService =(TypeService) ServiceProxyFactory.getService(TypeServiceImpl.class);
    private String basicUrl="/WEB-INF/jsp/main/";
    public void list(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        List<TType> list = typeService.queryAll();
        req.setAttribute("list",list);
        req.setAttribute("mainPage","./newsType/newsTypeList.jsp");
        req.getRequestDispatcher(basicUrl+"mainTemp.jsp").forward(req,resp);
    }
    public void addUI(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        req.setAttribute("mainPage","./newsType/newsTypeSave.jsp");
        req.getRequestDispatcher(basicUrl+"mainTemp.jsp").forward(req,resp);
    }
    public void save(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        String sid = req.getParameter("id");
        String typeName = req.getParameter("typeName");
        if(sid==null || sid.equals("")){
            TType type = new TType();
            type.setTypeName(typeName);
            typeService.add(type);
        }else {
            int id = Integer.parseInt(sid);
            TType type = new TType();
            type.setTypeName(typeName);
            type.setId(id);
            typeService.modify(type);
        }
        resp.sendRedirect(req.getContextPath()+"/type?method=list");
    }
    public void updateUI(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        int id = Integer.parseInt(req.getParameter("id"));
        TType type = typeService.queryTypeById(id);
        req.setAttribute("type",type);
        req.setAttribute("mainPage","./newsType/newsTypeSave.jsp");
        req.getRequestDispatcher(basicUrl+"mainTemp.jsp").forward(req,resp);
    }
    public void del(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        int id = Integer.parseInt(req.getParameter("id"));
        typeService.del(id);
        resp.sendRedirect(req.getContextPath()+"/type?method=list");
    }
}
